home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / APXMDIDV.PAK / APXPREV.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  9KB  |  326 lines

  1. //----------------------------------------------------------------------------
  2. //  Project ApxMdiDv
  3. //  Borland International
  4. //  Copyright ⌐ 1996. All Rights Reserved.
  5. //
  6. //  SUBSYSTEM:    ApxMdiDv Application
  7. //  FILE:         apxprev.cpp
  8. //  AUTHOR:       
  9. //
  10. //  OVERVIEW
  11. //  ~~~~~~~~
  12. //  Source file for implementation of Print Preview.
  13. //
  14. //----------------------------------------------------------------------------
  15. #include <owl/pch.h>
  16.  
  17. #include <owl/buttonga.h>
  18. #include <owl/textgadg.h>
  19. #include <stdio.h>
  20.  
  21. #include "apxprev.h"
  22. #include "apxmddva.rh"
  23.  
  24.  
  25. //{{TApxPreviewWin Implementation}}
  26.  
  27. DEFINE_RESPONSE_TABLE1(TApxPreviewWin, TDecoratedFrame)
  28.   EV_COMMAND_ENABLE(APX_PPR_PREVIOUS, PPR_PreviousEnable),
  29.   EV_COMMAND_ENABLE(APX_PPR_NEXT, PPR_NextEnable),
  30.   EV_COMMAND(APX_PPR_PREVIOUS, PPR_Previous),
  31.   EV_COMMAND(APX_PPR_NEXT, PPR_Next),
  32.   EV_COMMAND(APX_PPR_ONEUP, PPR_OneUp),
  33.   EV_COMMAND_ENABLE(APX_PPR_TWOUP, PPR_TwoUpEnable),
  34.   EV_COMMAND(APX_PPR_TWOUP, PPR_TwoUp),
  35.   EV_COMMAND(APX_PPR_DONE, PPR_Done),
  36.   EV_COMMAND(CM_FILEPRINT, CmPrint),
  37.   EV_COMMAND_ENABLE(CM_FILEPRINT, CmPrintEnable),
  38. //{{TApxPreviewWinRSP_TBL_BEGIN}}
  39.   EV_WM_CLOSE,
  40. //{{TApxPreviewWinRSP_TBL_END}}
  41. END_RESPONSE_TABLE;
  42.  
  43.  
  44. TApxPreviewWin::TApxPreviewWin(TWindow* parentWindow, TPrinter* printer,
  45.                                TWindow* currWindow, const char far* title,
  46.                                TLayoutWindow* client)
  47. :
  48.   TDecoratedFrame(parentWindow, title, client)
  49. {
  50.   CurrWindow = currWindow;
  51.   Printer = printer;
  52.   Client = client;
  53.   Page1 = 0;
  54.   Page2 = 0;
  55.   FromPage = 1;
  56.   ToPage = 1;
  57.  
  58.   TPrintDialog::TData& data = Printer->GetSetup();
  59.   PrnDC = new TPrintDC(data.GetDriverName(),
  60.                        data.GetDeviceName(),
  61.                        data.GetOutputName(),
  62.                        data.GetDevMode());
  63.  
  64.   PrintExtent = new TSize(PrnDC->GetDeviceCaps(HORZRES), PrnDC->GetDeviceCaps(VERTRES));
  65.   Printout = new TApxPrintout(Printer, "Print Preview", currWindow, true);
  66.  
  67.   SetBkgndColor(::GetSysColor(COLOR_APPWORKSPACE));
  68.  
  69.   // Create default toolbar New and associate toolbar buttons with commands.
  70.   //
  71.   PreviewSpeedBar = new TControlBar(this);
  72.   PreviewSpeedBar->Insert(*new TButtonGadget(APX_PPR_PREVIOUS, APX_PPR_PREVIOUS, TButtonGadget::Command, true));
  73.   PreviewSpeedBar->Insert(*new TButtonGadget(APX_PPR_NEXT, APX_PPR_NEXT, TButtonGadget::Command, true));
  74.   PreviewSpeedBar->Insert(*new TSeparatorGadget(6));
  75.   PreviewSpeedBar->Insert(*new TButtonGadget(APX_PPR_ONEUP, APX_PPR_ONEUP, TButtonGadget::Exclusive, true, TButtonGadget::Down));
  76.   PreviewSpeedBar->Insert(*new TButtonGadget(APX_PPR_TWOUP, APX_PPR_TWOUP, TButtonGadget::Exclusive, true));
  77.   PreviewSpeedBar->Insert(*new TSeparatorGadget(12));
  78.   PreviewSpeedBar->Insert(*new TTextGadget(APX_PPR_CURRPAGE, TGadget::Recessed, TTextGadget::Left, 10, "Page 1"));
  79.   PreviewSpeedBar->Insert(*new TSeparatorGadget(20));
  80.   PreviewSpeedBar->Insert(*new TButtonGadget(CM_FILEPRINT, CM_FILEPRINT, TButtonGadget::Command, true));
  81.   PreviewSpeedBar->Insert(*new TSeparatorGadget(20));
  82.   PreviewSpeedBar->Insert(*new TButtonGadget(APX_PPR_DONE, APX_PPR_DONE, TButtonGadget::Command, true));
  83.   Insert(*PreviewSpeedBar, TDecoratedFrame::Top);
  84.  
  85.   Attr.Style &= ~WS_VISIBLE;
  86.   SetAcceleratorTable(IDM_PRINTPREVIEW);
  87. }
  88.  
  89.  
  90. TApxPreviewWin::~TApxPreviewWin()
  91. {
  92.   delete Page1;
  93.   delete Page2;
  94.  
  95.   delete PrnDC;
  96.   delete PrintExtent;
  97.   delete Printout;
  98. }
  99.  
  100.  
  101. void TApxPreviewWin::SetupWindow()
  102. {
  103.   TDecoratedFrame::SetupWindow();
  104.  
  105.   TAPointer<char> captionText = new char[256];
  106.  
  107.   // Set the caption of the preview window based on that of the Main Window.
  108.   //
  109.   GetApplication()->GetMainWindow()->GetWindowText(captionText, 256);
  110.   strcat(captionText, " (Preview)");
  111.   SetCaption(captionText);
  112.  
  113.   // Set the icons of the preview window.
  114.   //
  115.   SetIcon(GetApplication(), IDI_MDIAPPLICATION);
  116.   SetIconSm(GetApplication(), IDI_MDIAPPLICATION);
  117.  
  118.   TPrintDialog::TData& data = Printer->GetSetup();
  119.   Page1 = new TPreviewPage(Client, *Printout, *PrnDC, *PrintExtent, 1);
  120.   Page1->SetPageNumber(1);
  121.   data.MaxPage = 1;
  122.  
  123.   Page2 = 0;
  124.  
  125.   TLayoutMetrics metrics1;
  126.  
  127.   metrics1.X.Set(lmLeft, lmRightOf, lmParent, lmLeft, 15);
  128.   metrics1.Y.Set(lmTop, lmBelow, lmParent, lmTop, 15);
  129.  
  130.   // Determine major axis of preview page, have that follow parent size.
  131.   // Make minor axis a percentage (aspect ratio) of the page's major axis
  132.   //
  133.   TRect r = Client->GetClientRect();
  134.   long ratio;
  135.  
  136.   if (PrintExtent->cx > PrintExtent->cy)
  137.     ratio = ((long)PrintExtent->cy * 100) / PrintExtent->cx;
  138.   else
  139.     ratio = ((long)PrintExtent->cx * 100) / PrintExtent->cy;
  140.  
  141.   bool xMajor = ((r.Width() * ratio) / 100) > r.Height();
  142.   if (xMajor){
  143.     metrics1.Height.Set(lmBottom, lmAbove, lmParent, lmBottom, 15);
  144.     metrics1.Width.PercentOf(Page1, (int)((long)PrintExtent->cx * 95 / PrintExtent->cy), lmHeight);
  145.   }
  146.   else {
  147.     metrics1.Height.PercentOf(Page1, (int)((long)PrintExtent->cy * 95 / PrintExtent->cx), lmWidth);
  148.     metrics1.Width.Set(lmRight, lmLeftOf, lmParent, lmRight, 15);
  149.   }
  150.  
  151.   Page1->Create();
  152.  
  153.   Client->SetChildLayoutMetrics(*Page1, metrics1);
  154.   Client->Layout();
  155. }
  156.  
  157.  
  158. void TApxPreviewWin::SpeedBarState()
  159. {
  160.   // Update the page count.
  161.   //
  162.   TTextGadget* cpGadget = TYPESAFE_DOWNCAST(PreviewSpeedBar->GadgetWithId(APX_PPR_CURRPAGE), TTextGadget);
  163.   if (cpGadget) {
  164.     TAPointer<char> buffer = new char[32];
  165.  
  166.     if (Page2 && FromPage != ToPage)
  167.       sprintf(buffer, "Page %d - %d", FromPage, ToPage);
  168.     else
  169.       sprintf(buffer, "Page %d", FromPage);
  170.     cpGadget->SetText(buffer);
  171.   }
  172. }
  173.  
  174.  
  175. void TApxPreviewWin::PPR_PreviousEnable(TCommandEnabler& tce)
  176. {
  177.   // Only have previous on if we're not at the first page.
  178.   //
  179.   tce.Enable(FromPage != 1);
  180. }
  181.  
  182.  
  183. void TApxPreviewWin::PPR_NextEnable(TCommandEnabler& tce)
  184. {
  185.   // Only have next on if we're not at the last page.
  186.   //
  187.   TPrintDialog::TData& printerData = Printer->GetSetup();
  188.   tce.Enable(ToPage != printerData.MaxPage);
  189. }
  190.  
  191.  
  192. void TApxPreviewWin::PPR_Previous()
  193. {
  194.   TPrintDialog::TData& printerData = Printer->GetSetup();
  195.  
  196.   if (FromPage > printerData.MinPage) {
  197.     FromPage--;
  198.     ToPage--;
  199.  
  200.     Page1->SetPageNumber(FromPage);
  201.     if (Page2)
  202.       Page2->SetPageNumber(ToPage);
  203.   }
  204.  
  205.   SpeedBarState();
  206. }
  207.  
  208.  
  209. void TApxPreviewWin::PPR_Next()
  210. {
  211.   TPrintDialog::TData& printerData = Printer->GetSetup();
  212.  
  213.   if (ToPage < printerData.MaxPage) {
  214.     FromPage++;
  215.     ToPage++;
  216.  
  217.     Page1->SetPageNumber(FromPage);
  218.     if (Page2)
  219.       Page2->SetPageNumber(ToPage);
  220.   }
  221.  
  222.   SpeedBarState();
  223. }
  224.  
  225.  
  226. void TApxPreviewWin::PPR_OneUp()
  227. {
  228.   if (Page2) {
  229.     Client->RemoveChildLayoutMetrics(*Page2);
  230.  
  231.     delete Page2;
  232.     Page2 = 0;
  233.  
  234.     Client->Layout();
  235.  
  236.     ToPage = FromPage;
  237.  
  238.     SpeedBarState();
  239.   }
  240. }
  241.  
  242.  
  243. void TApxPreviewWin::PPR_TwoUpEnable(TCommandEnabler& tce)
  244. {
  245.   // Two up is only available for portrait mode.
  246.   //
  247.   tce.Enable(PrintExtent->cx <= PrintExtent->cy);
  248. }
  249.  
  250.  
  251. void TApxPreviewWin::PPR_TwoUp()
  252. {
  253.   if (!Page2) {
  254.     Page2 = new TPreviewPage(Client, *Printout, *PrnDC, *PrintExtent, PageNumber + 1);
  255.     Page2->Create();
  256.  
  257.     TLayoutMetrics metrics2;
  258.  
  259.     metrics2.X.Set(lmLeft, lmRightOf, Page1, lmRight, 30);
  260.     metrics2.Y.SameAs(Page1, lmTop);
  261.  
  262.     // Assume portrait
  263.     //
  264.     metrics2.Width.SameAs(Page1, lmWidth);
  265.     metrics2.Height.SameAs(Page1, lmBottom);
  266.  
  267.     Client->SetChildLayoutMetrics(*Page2, metrics2);
  268.     Client->Layout();
  269.  
  270.     TPrintDialog::TData& printerData = Printer->GetSetup();
  271.  
  272.     // Page 2 is the next page.  If the next page is outside of our range then
  273.     // set the first page back one and the 2nd page is the current page. If the
  274.     // document is only 1 page long then the 2nd page is empty.
  275.     //
  276.     if (FromPage == printerData.MaxPage) {
  277.       if (FromPage > 1) {
  278.         FromPage--;
  279.         ToPage = FromPage + 1;
  280.         Page1->SetPageNumber(FromPage);
  281.         Page2->SetPageNumber(ToPage);
  282.       }
  283.       else
  284.         Page2->SetPageNumber(0);
  285.     }
  286.     else {
  287.       ToPage = FromPage + 1;
  288.       Page2->SetPageNumber(ToPage);
  289.     }
  290.  
  291.     SpeedBarState();
  292.   }
  293. }
  294.  
  295.  
  296. void TApxPreviewWin::PPR_Done()
  297. {
  298.   // Don't call the base class EvClose; we do not want TApxPreviewWin to be destructed.
  299.   //
  300.   GetApplication()->EndModal(IDCANCEL);
  301. }
  302.  
  303.  
  304. void TApxPreviewWin::EvClose()
  305. {
  306.   // Don't call the base class EvClose; we do not want TApxPreviewWin to be destructed.
  307.   //
  308.   GetApplication()->EndModal(IDCANCEL);
  309. }
  310.  
  311.  
  312. void TApxPreviewWin::CmPrint()
  313. {
  314.   TWindow* client = GetApplication()->GetMainWindow()->GetClientWindow();
  315.  
  316.   if (client)
  317.     client->SendMessage(WM_COMMAND, CM_FILEPRINT, 0);
  318. }
  319.  
  320.  
  321. void TApxPreviewWin::CmPrintEnable(TCommandEnabler& tce)
  322. {
  323.   tce.Enable(true);
  324. }
  325.  
  326.